<?php
!defined('QAPP') AND define('QAPP', 1);
define('admin', true);
$start_time = microtime(true);
include CoreDir . 'admin_func.php';
include 'hooks.php';
if (session_get("goto") != "") {
    $gotourl = session_get("goto");
    session_set("goto", "");
    header("Location:" . $gotourl);
    exit();
}


$ucms_admin = getadminname();
$ucms_psw = $_COOKIE['psw_' . cookiehash];
$query = M()->query("SELECT * FROM " . tableex('admin') . " where username='" . $ucms_admin . "' limit 1");
$admin_user_info = M()->fetchOne();
if ($ucms_psw != md5($admin_user_info['psd'] . SiteHash)) {
    if (is_ajax_request()) {
        exit(resp_error_json('请先登录'));
    }
    admin_error();
}
$power = json_decode($admin_user_info['power'], 1);
$mynickname = $admin_user_info['nickname'];
$myadminuid = $admin_user_info['id'];

if (!power('b', 1)) {
    if (is_ajax_request()) {
        exit(resp_error_json('该账户已禁止登录'));
    }
    admin_error('该账户已禁止登录');
}


//die;
newtoken(10);
$thisdo = array('', '');
if (isset($_GET['do'])) {
    if (isset($_GET['do'])) {
        $thisdo = explode('_', $_GET['do']);
    }

    if (!isset($thisdo[1])) {
        $thisdo[1] = 'index';
    }
}
//global $power;
//var_dump($power);
//加载css、js等资源，不带下方的html头部
if ($thisdo[0] && (isset($_GET['nohtml']) || isset($_GET['ajax']) || is_ajax_request())) {
    $admin_file = load_admin_file($thisdo[0], $thisdo[1]);
    if ($admin_file) {
        require $admin_file;
    } else {
        exit(resp_error_json("not found adminpage"));
    }
    exit();
}
?>
<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>后台管理中心</title>
        <meta name="referrer" content="origin-when-cross-origin">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

        <link href="./adminassets/bootstrap.css" rel="stylesheet" type="text/css">
        <link href="./adminassets/admin.css" rel="stylesheet" type="text/css">
        <link href="./adminassets/qapp-admin.css" rel="stylesheet" type="text/css">
        <script src="./adminassets/jquery-1.12.4.min.js"></script>
        <script src="./adminassets/bootstrap.js"></script>
        <script src="./adminassets/js.js"></script>
        <script src="./adminassets/jtool.js"></script>
        <script type="text/javascript">cmsversion = '<?php echo(version); ?>';
            console.log("version:" + cmsversion);</script>
        <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1"/>
        <meta name="renderer" content="webkit">
        <?php
        getHooks()->do_action('admin_assest');
        ?>
    </head>
    <body style="display: table">
        <?php
        $channels = adminchannelscache();
        ?>

        <style>

            .LeftMenuShowBase {
                width: 200px;
                padding: 10px;
            }

            .LeftMenuShow {
                display: table-cell;
            }

            .sidemove {
                -webkit-transition: all 0.3s ease;
                transition: all 0.3s ease;
            }
        </style>

        <div class="sidemove navbar-inverse LeftMenuShowBase LeftMenuShow"
             id="ULeft" style="<?= cookie_get('lefthide') ? 'display:none' : '' ?>">
            <div style="">
                <div style="text-align: center;margin: 10px;">
                    <?php
                    if (uuu_cfg("网站标题")) {
                        $admin_title = str_cut_addtext(uuu_cfg("网站标题"), 10, '...');
                    } else {
                        $admin_title = "网站后台";
                    }

                    if (uuu_cfg("logo图片")) {
                        $admin_logo = uuu_cfg("logo图片");
                    } else {
                        $admin_logo = "/logo.png";
                    }

                    getHooks()->do_action("admin_logo_before");
                    ?>
                    <a style="color: #fff" class="" href="./">
                        <img style="height: 60px" src="<?= $admin_logo ?>">
                        <p style="margin: 10px 0"><?= $admin_title ?></p>
                    </a>
                </div>
                <?php
                if (isset($_GET['cid'])) {
                    $cid = intval($_GET['cid']);
                } else {
                    $cid = 0;
                }
                $mydaddy[0] = 1;
                getHooks()->do_action('admin_left_before');
                echo "<ul class=''><hr>";
                if (power('s', 0)) {
                    $ifon = "";
                    if (isset($thisdo[0]) && $thisdo[0] == 'str' && $cid == 0)
                        $ifon = "active";
                    $mynav_setting = array('url' => '?do=str', 'title' => "系统设置", 'cid' => 0, 'fid' => 0, 'blank' => '');
                    echo("<a href='?do=str'><li class='{$ifon}' rel='0'>系统设置</li></a>");
                }
                echo "</ul>";
                getleftlist();
                getHooks()->do_action('admin_left_after');
                ?>
            </div>
        </div>
        <div style="display: table-cell;vertical-align: top;" class="sidemove">
            <div id="UHead">
                <div id="head" class="navbar navbar-default">
                    <a href="#" id="menuToggle" onclick="return menuToggle()" class="menu_toggle pull-left"><i
                            class="glyphicon glyphicon-list"></i></a>

                    <ul id="topMenu">
                        <a href="<?php echo(qqq_homeurl()); ?>" target="_blank">
                            <li>前台</li>
                        </a>
                        <?php
                        if (power('b', 2)) {
                            echo('<a href="?do=user_my"><li>个人</li></a>');
                        }
                        if (power('alevel') > 1) {
                            echo('<a href="?do=user"><li>帐户</li></a>');
                            echo('<a onclick="return confirm(\'确定清除缓存？\')" href="?do=str_cache&' . newtoken(3) . '"><li>清除缓存</li></a>');
                            if (MLang) {
                                echo('<a href="?do=lang"><li>多语言</li></a>');
                            }
                            echo('<a href="?do=backup"><li>数据</li></a>');
                        }
                        if (power('alevel') == 3) {
                            echo('<a href="?do=sadmin_sbasic"><li>变量</li></a>');
                            echo('<a href="?do=sadmin_url"><li>路由</li></a>');
                            echo('<a href="?do=sadmin"><li>栏目</li></a>');
                            echo('<a href="?do=list&cid=14"><li>日志</li></a>');
                        }
                        ?>
                        <a onclick="return confirm('确定要退出系统？')" href="login.php?do=out&<?php echo(newtoken(2)); ?>">
                            <li>退出</li>
                        </a>
                    </ul>
                </div>
            </div>
            <?php
            if ($thisdo[0]) {
                $admin_file = load_admin_file($thisdo[0], $thisdo[1]);
                if ($admin_file) {
                    require $admin_file;
                } else {
                    adminmsg('', "not found adminpage");
                }
            } else {
                if (power('s', 0, $power)) {
                    $gotourl = '?do=str';
                    getHooks()->do_action("admin_empty_do_goto");
                    resp_goto_url($gotourl);
                    exit();
                }
                foreach ($channels as $value) {
                    if (power('s', $value['cid'])) {
                        if ($value['ckind'] == 1 || $value['ckind'] == 3) {
                            $firsturl = '?do=str&cid=' . $value['cid'];
                        }
                        if ($value['ckind'] == 2) {
                            $firsturl = '?do=list&cid=' . $value['cid'];
                        }
                        if ($value['ckind'] == 4) {
                            if ($value['newwindow'] != 1) {
                                $firsturl = $value['cvalue'];
                            }
                        }
                        break;
                    }
                }
                if (isset($firsturl)) {
                    echo("<meta http-equiv=refresh content='0; url=$firsturl'>");
                    exit();
                }
            }
            ?>
        </div>
        <footer class="navbar-fixed-bottom navbar-inverse">
            <div class="navbar-text text-center" style="display: block;float: none;margin: 5px 10px;text-align: right">
                &copy;<?php echo(date('Y')); ?> <a
                    href="https://gitee.com/withu2018/qapp" target="_blank">QAPP</a> <?php
                    if (uuu_cfg("开发模式")) {
                        $end_time = microtime(true);
                        $total_time = substr($end_time - $start_time, 0, 8);
                        echo " " . $total_time*1000 . 'ms';
                    } else {
                        echo "/ base <a href='http://uuu.la' target='_blank'>UCMS</a>";
                    }
                    ?></div>
        </footer>
        <div class="clear"></div>
        <script type="text/javascript">
            var left_is_show = <?= !cookie_get('lefthide') ? 'true' : 'false' ?>;
            function IsPC() {
                var userAgentInfo = navigator.userAgent;
                var Agents = ["Android", "iPhone",
                    "SymbianOS", "Windows Phone",
                    "iPad", "iPod"];
                var flag = true;
                for (var v = 0; v < Agents.length; v++) {
                    if (userAgentInfo.indexOf(Agents[v]) > 0) {
                        flag = false;
                        break;
                    }
                }
                return flag;
            }
            function menuToggle() {
                if (!left_is_show) {

                    $("#ULeft").show();
                    $("#ULeft").width(180)
                    setTimeout(function () {
                        $("#ULeft>div").show();
                    }, 100)
                    setCookie("lefthide", 0);
                } else {
                    $("#ULeft>div").hide();
                    $("#ULeft").width(0)
                    setTimeout(function () {
                        $("#ULeft").fadeOut(100)
                    }, 200)
                    //
                    setCookie("lefthide", 1);
                }
                left_is_show = !left_is_show;

                return false;
            }


            $(document).keydown(function (event) {
                var ctrlc = event.which;
                if (ctrlc == 13 && event.ctrlKey) {
                    $('#form1').submit();
                }
                if (event.altKey) {
<?php
if (isset($_GET['cid']) && $_GET['cid'] > 0) {
    if (power('s', intval($_GET['cid']), $power, 1)) {
        echo('if(ctrlc == 65){window.location.href = "?do=list_add&cid=' . intval($_GET['cid']) . '";}');
    }
    if (power('s', intval($_GET['cid']), $power, 4)) {
        echo('if(ctrlc == 83){window.location.href = "?do=str&cid=' . intval($_GET['cid']) . '";}');
    }
    if (power('s', intval($_GET['cid']))) {
        echo('if(ctrlc == 86){window.location.href = "?do=list&cid=' . intval($_GET['cid']) . '";}');
    }
    if (power('alevel') == 3) {
        echo('if(ctrlc == 90){window.location.href = "?do=sadmin_cedit&cid=' . intval($_GET['cid']) . '";}');
        echo('if(ctrlc == 88){window.location.href = "?do=sadmin_aindex&cid=' . intval($_GET['cid']) . '";}');
        echo('if(ctrlc == 67){window.location.href = "?do=sadmin_sbasic&cid=' . intval($_GET['cid']) . '";}');
    }
} elseif (!isset($_GET['cid']) && power('alevel') == 3) {
    echo('if(ctrlc == 67){window.location.href = "?do=sadmin_sbasic";}');
    echo('if(ctrlc == 90){window.location.href = "?do=sadmin";}');
}
?>
                    if (ctrlc == 72) {
                        window.open('<?php echo(SystemDir); ?>');
                    }
                }
            });
            $(function () {
                $('#form1').find("table").eq(0).find("tr").eq(0).find('input[type!=hidden]').eq(0).focus();
                $('#form1').find("table").eq(0).find("tr").eq(0).find('textarea').eq(0).focus();
                $('#form1').find("table").eq(0).find("tr").eq(0).find('select').eq(0).focus();

                if(left_is_show && !IsPC()){
                    menuToggle()
                }
            });
        </script>
    </body>
</html>
